﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //页面第一次加载的时候，填充数据
            if (!IsPostBack)
            {
                FillData();
            }
        }

        //为GridView填充数据
        private void FillData()
        {
            var sql = "select * from users";
            var dt = DbHelper.GetData(sql);
            gv4.DataSource = dt;
            gv4.DataBind();
        }

        protected void gv4_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv4.EditIndex = -1;
            FillData();
        }

        protected void gv4_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);
            var sql = string.Format("delete from users where id={0}", id);
            DbHelper.Exe(sql);

            gv4.EditIndex = -1;
            FillData();
        }

        protected void gv4_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv4.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void gv4_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetValue(e.RowIndex, 0);

            var isOk = int.TryParse(idString, out int id);
            var uid = GetValue(e.RowIndex, 1).Replace("&nbsp;","");
            var pwd = GetValue(e.RowIndex, 2).Replace("&nbsp;", "");
            var remarks = GetValue(e.RowIndex, 3).Replace("&nbsp;", "");

            if (isOk)
            {
                //更新
                var sql1 = string.Format("update users set username='{0}',password='{1}',remarks='{2}' where id={3}", uid, pwd, remarks, id);
                DbHelper.Exe(sql1);
            }
            else
            {
                //添加
                var sql2 = string.Format("insert into users (username,password,remarks) values ('{0}','{1}','{2}')", uid, pwd, remarks);
                DbHelper.Exe(sql2);
            }            

            gv4.EditIndex = -1;
            FillData();
        }

        private string GetValue(int rowIndex, int colIndex)
        {

            //这是一个单元格 服务器控件
            var c = gv4.Rows[rowIndex].Cells[colIndex];
            //var value = c.Controls.Count > 0 ? ((TextBox)c.Controls[0]).Text : c.Text;

            string value = string.Empty;
            if (c.Controls.Count > 0)
            {
                var t = c.Controls[0];
                var txtBox=(TextBox)t;
                value = txtBox.Text;
            }
            else
            {
                value = c.Text;
            }

            return value;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id"));
            dt.Columns.Add(new DataColumn("Username"));
            dt.Columns.Add(new DataColumn("Password"));
            dt.Columns.Add(new DataColumn("Remarks"));

            foreach(GridViewRow row in gv4.Rows)
            {
                var newRow = dt.NewRow();

                newRow["Id"] = GetValue(row.RowIndex, 0);
                newRow["Username"] = GetValue(row.RowIndex, 1).Replace("&nbsp;","");
                newRow["Password"] = GetValue(row.RowIndex, 2).Replace("&nbsp;", ""); 
                newRow["Remarks"] = GetValue(row.RowIndex, 3).Replace("&nbsp;", "");

                dt.Rows.Add(newRow);
            }

            dt.Rows.Add(dt.NewRow());

            gv4.DataSource = dt;
            gv4.DataBind();

            gv4.EditIndex = dt.Rows.Count - 1;


            gv4.DataSource = dt;
            gv4.DataBind();
        }
    }
}